/***************************************************************************
 * Copyright (C) 2008 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.agi.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;
import org.appfuse.model.User;

/**
 * 
 * @author trobertson
 */
@Entity
@Table(name="genus_relationship")
public class GenusRelationship extends BaseObject implements Comparable<Object> {
	private static final long serialVersionUID = -3738514096510977475L;
	
	private Long id;
	private RelationshipType relationshipType;
	private Genus fromGenus;
	private Genus toGenus;
	private AGIDatasource agiDatasource;
	private User user;
	private Algorithm algorithm;
	private Date created = new Date();
	private Date updated = new Date();
	private Date deleted;
	private User deletionUser;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(columnDefinition="int")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn (name="datasource_id")
	public AGIDatasource getAgiDatasource() {
		return agiDatasource;
	}
	public void setAgiDatasource(AGIDatasource agiDatasource) {
		this.agiDatasource = agiDatasource;
	}
	
	@Column(nullable=false)
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	
	@Column(nullable=false)
	public Date getUpdated() {
		return updated;
	}
	public void setUpdated(Date updated) {
		this.updated = updated;
	}
	
	@ManyToOne
	@JoinColumn (name="user_id")
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	@ManyToOne (optional=false)
	@JoinColumn (name="from_genus_id")
	public Genus getFromGenus() {
		return fromGenus;
	}
	public void setFromGenus(Genus fromGenus) {
		this.fromGenus = fromGenus;
	}
	
	@ManyToOne (optional=false)
	@JoinColumn (name="to_genus_id")
	public Genus getToGenus() {
		return toGenus;
	}
	public void setToGenus(Genus toGenus) {
		this.toGenus = toGenus;
	}
	
	@ManyToOne (optional=false)
	@JoinColumn (name="type_id")
	public RelationshipType getRelationshipType() {
		return relationshipType;
	}
	public void setRelationshipType(RelationshipType relationshipType) {
		this.relationshipType = relationshipType;
	}
	
	@ManyToOne
	@JoinColumn (name="algorithm_id")
	public Algorithm getAlgorithm() {
		return algorithm;
	}
	public void setAlgorithm(Algorithm algorithm) {
		this.algorithm = algorithm;
	}
	
	public Date getDeleted() {
		return deleted;
	}
	public void setDeleted(Date deleted) {
		this.deleted = deleted;
	}
	
	@ManyToOne
	@JoinColumn (name="deletion_user_id")	
	public User getDeletionUser() {
		return deletionUser;
	}
	public void setDeletionUser(User deletionUser) {
		this.deletionUser = deletionUser;
	}
	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		GenusRelationship myClass = (GenusRelationship) object;
		return new CompareToBuilder().append(this.deleted, myClass.deleted)
				.append(this.created, myClass.created).append(
						this.relationshipType, myClass.relationshipType)
				.append(this.user, myClass.user).append(this.fromGenus,
						myClass.fromGenus)
				.append(this.toGenus, myClass.toGenus).append(
						this.agiDatasource, myClass.agiDatasource).append(
						this.deletionUser, myClass.deletionUser).append(
						this.updated, myClass.updated).append(this.algorithm,
						myClass.algorithm).append(this.id, myClass.id)
				.toComparison();
	}
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof GenusRelationship)) {
			return false;
		}
		GenusRelationship rhs = (GenusRelationship) object;
		return new EqualsBuilder().append(this.deleted, rhs.deleted).append(
				this.created, rhs.created).append(this.relationshipType,
				rhs.relationshipType).append(this.user, rhs.user).append(
				this.fromGenus, rhs.fromGenus)
				.append(this.toGenus, rhs.toGenus).append(this.agiDatasource,
						rhs.agiDatasource).append(this.deletionUser,
						rhs.deletionUser).append(this.updated, rhs.updated)
				.append(this.algorithm, rhs.algorithm).append(this.id, rhs.id)
				.isEquals();
	}
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-605332253, -137828545).append(this.deleted)
				.append(this.created).append(this.relationshipType).append(
						this.user).append(this.fromGenus).append(this.toGenus)
				.append(this.agiDatasource).append(this.deletionUser).append(
						this.updated).append(this.algorithm).append(this.id)
				.toHashCode();
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("updated", this.updated)
				.append("created", this.created).append("agiDatasource",
						this.agiDatasource).append("id", this.id).append(
						"deletion_user", this.deletionUser).append("deleted",
						this.deleted).append("toGenus", this.toGenus).append(
						"user", this.user).append("algorithm", this.algorithm)
				.append("relationshipType", this.relationshipType).append(
						"fromGenus", this.fromGenus).toString();
	}
}
